package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.SeasonCompetionTeamPlayerDao;
import cz.khfotbal.domain.Player;
import cz.khfotbal.domain.SeasonCompetionTeam;
import cz.khfotbal.domain.SeasonCompetionTeamPlayer;

@Repository
@Transactional
public class SeasonCompetionTeamPlayerDaoImpl implements
		SeasonCompetionTeamPlayerDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

	public List<SeasonCompetionTeamPlayer> findBySeasonCompetionTeam(
			SeasonCompetionTeam seasonCompetionTeam) {
		session = sessionFactory.getCurrentSession();
		List<SeasonCompetionTeamPlayer> result = session.createQuery(
				" from SeasonCompetionTeamPlayer sctp where sctp.seasonCompetionTeam.id="
						+ seasonCompetionTeam.getId()).list();
		return result;
	}

	public void save(SeasonCompetionTeamPlayer seasonCompetionTeamPlayer) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(seasonCompetionTeamPlayer);
	}

	public SeasonCompetionTeamPlayer findBySCTAndPlayer(
			SeasonCompetionTeam sct, Player player) {
		session = sessionFactory.getCurrentSession();
		return (SeasonCompetionTeamPlayer) session
				.createQuery(
						" from SeasonCompetionTeamPlayer sctp where sctp.seasonCompetionTeam.id="
								+ sct.getId() + " and sctp.player.id="
								+ player.getId()).uniqueResult();
	}

	public void delete(SeasonCompetionTeamPlayer sctp) {
		session = sessionFactory.getCurrentSession();
		session.delete(sctp);
	}
}
